<?php
namespace model;

use utils\PathTool;
use utils\UtilsTool;
use biz\FolderSchema;
/*
	说明：
		1.在调用此函数前不能有任何输出操作。比如 echo print
		
	更新记录：
		2014-08-12 创建
*/
class FileInf 
{	
	var $id         = "";       // 本地文件ID。
	var $pid        = "";       // 服务端父ID(文件夹在数据库中的ID)
	var $pidRoot    = "";       // 根级文件夹ID，数据库ID，与xdb_folders.fd_id对应
	var $fdTask     = false;
	var $blockCount = 0;
	var $blockSize  = 0;
	var $blockIndex = 0;        //块索引，基于1
	var $blockOffset= 0;        // 块偏移,基于整个文件
	var $fileCount  = 0;
	var $blockPath  = "";
	var $fdChild    = false;
	var $uid        = "";        // 用户ID	
	var $nameLoc    = "";       // 文件名称。示例：QQ2014.exe
	var $nameSvr    = "";
	var $pathLoc    = "";       // 文件在客户端中的路径。示例：D:\\Soft\\QQ2013.exe
	var $pathSvr    = "";       // 文件在服务器上面的路径。示例：E:\\Web\\Upload\\QQ2013.exe
	var $pathRel    = "";	
	var $lenLoc     = 0;        // 数字化的长度。以字节为单位，示例：1021021
	var $lenLocSec  = 0;        // 加密后的文件大小
	var $sizeLoc    = "0bytes"; // 格式化的长度。示例：10G
	var $sizeSvr	= "0bytes";
	var $perSvr     = "0%";     // 上传百分比
	var $lenSvr     = 0;        // 已上传大小
	var $md5        = "";       // 文件MD5
	var $complete   = false;
	var $PostedTime;
	var $deleted	= false;
	var $scaned     = false;
	var $storage    ="io";
	var $objectKey  ="";
	var $objectUrl  ="";
	var $object_id  ="";
	var $encrypt    = false;
	var $encryptAlgorithm = "aes";//加密算法类型,aes,sm4
	var $blockEncrypt = false;////块加密标识
	var $blockSizeCry = 0;//块加密大小
	var $blockSizeCpr = 0;//块压缩大小
	var $blockCompress= false;//块压缩
	var $blockCompressType ="gzip";
	
	function __construct()
	{
		date_default_timezone_set("PRC");//fix(2016-12-06):在部分server中提示警告
		$this->PostedTime = date("Y-m-d H:i:s");
	}
	
	/// <summary>
	/// 计算文件加密后的大小，16字节对齐
	/// </summary>
	public function calLenLocSec() {
	    $a = $this->lenLoc % 16;
	    $this->lenLocSec = $this->lenLoc + (16 - ($a > 0 ? $a : 16));
	    
	    if (0 == strcasecmp($this->encryptAlgorithm, "sm4"))
	    {
	        $this->calLenLocSecSm4();
	    }
	}
	
	/// <summary>
	/// sm4-cbc-pkcs5padding
	/// (块大小* 块对齐大小) + 最后一块对齐大小
	/// </summary>
	public function calLenLocSecSm4() {
	    $a = $this->lenLoc % 16;
	    $this->lenLocSec = $this->lenLoc + (16 - ($a > 0 ? $a : 16));
	    
	    if (0 == strcasecmp($this->encryptAlgorithm, "sm4"))
	    {
	        $this->calLenLocSecSm4();
	    }
	    
	    //每一块对齐大小
	    $this->blockSizeCry = UtilsTool::sm4AlignBlockSize($this->blockSize);
	    
	    //块总数-1 * 块对齐大小
	    $this->lenLocSec = ($this->blockCount-1) * $this->blockSizeCry;
	    
	    //最后一块对齐大小
	    $lastSize = $this->lenLoc % $this->blockSize;
	    $lastSize = UtilsTool::sm4AlignBlockSize($lastSize);
	    
	    $this->lenLocSec += $lastSize;
	}	
	
	/**
	 * 生成层级信息文件路径
	 */
	public function schemaFile()
	{
	    return PathTool::combin($this->parentDir(),"schema.txt");
	}
	
	public function parentDir()
	{
	    return dirname($this->pathSvr);
	}
	
	/**
	 * 保存到层级信息文件
	 */
	public function saveScheme()
	{
	    $fs = new FolderSchema();
	    $this->calLenLocSec();//自动计算文件加密后的大小
	    $fs->addFile($this);
	}
	
	//解析
	public function parseSchema(/*string*/$line)
	{
	    $v = json_decode($line,true);//to array map
	    $this->id = $v["id"];
	    $this->pid = $v["pid"];
	    $this->pidRoot = $v["pidRoot"];
	    $this->fdTask = $v["fdTask"];
	    $this->fdChild = $v["fdChild"];
	    $this->uid = $v["uid"];
	    $this->nameLoc = $v["nameLoc"];
	    $this->nameSvr = $v["nameSvr"];
	    $this->pathLoc = $v["pathLoc"];
	    $this->pathSvr = $v["pathSvr"];
	    $this->pathRel = $v["pathRel"];
	    $this->md5 = $v["md5"];
	    $this->lenLoc = $v["lenLoc"];
	    $this->lenLocSec = $v["lenLocSec"];
	    $this->sizeLoc = $v["sizeLoc"];
	    $this->blockOffset = $v["blockOffset"];
	    $this->lenSvr = $v["lenSvr"];
	    $this->perSvr = $v["perSvr"];
	    $this->complete = $v["complete"];
	    $this->PostedTime = $v["PostedTime"];
	    $this->blockIndex = $v["blockIndex"];
	    $this->blockCount = $v["blockCount"];
	    $this->blockSize = $v["blockSize"];
	    $this->object_id = $v["object_id"];
	    $this->encrypt = $v["encrypt"];
	    $this->encryptAlgorithm = $v["encryptAlgorithm"];
	    $this->storage = $v["storage"];
	    $this->objectKey = $v["objectKey"];
	    $this->objectUrl = $v["objectUrl"];	    
	}
	
	//解析子文件信息
	public function parseSchemaChild(/*string*/$line)
	{
	    $v = json_decode($line,true);//to array map
	    $this->id = $v["id"];
	    $this->pid = $v["pid"];
	    $this->pidRoot = $v["pidRoot"];
	    $this->nameLoc = $v["nameLoc"];
	    $this->nameSvr = $v["nameSvr"];
	    $this->pathLoc = $v["pathLoc"];
	    $this->pathSvr = $v["pathSvr"];
	    $this->pathRel = $v["pathRel"];
	    $this->lenLoc = $v["lenLoc"];
	    $this->lenLocSec = $v["lenLocSec"];
	    $this->sizeLoc = $v["sizeLoc"];
	    $this->lenSvr = $v["lenSvr"];
	    $this->objectKey = $v["objectKey"];
	    $this->objectUrl = $v["objectUrl"];
	    $this->storage = $v["storage"];
	}
}
?>